package br.com.midiaseducativas.tutor.jogos.quiz {
	import br.com.midiaseducativas.tutor.jogos.quiz.event.CheckGroupEvent;
	import br.com.midiaseducativas.tutor.jogos.quiz.vo.QuestionVO;
	
	import com.demonsters.debugger.MonsterDebugger;
	
	import flash.display.StageQuality;
	import flash.events.Event;
	
	
	public class Question {
		
		private var _vo:QuestionVO;
		private var _answers:Array;
		
		public function Question(pVO:QuestionVO) {
			this.vo = pVO;
			this.answers = new Array();
			
		}
		
		public function get answers():Array
		{
			return _answers;
		}

		public function set answers(value:Array):void
		{
			_answers = value;
		}

		public function get vo():QuestionVO
		{
			return _vo;
		}

		public function set vo(value:QuestionVO):void
		{
			_vo = value;
		}

		/**
		 * Função que monta as questões
		 * @return void
		 * @author Caio Neves
		 **/

		public function buildQuest(quizStage:QuizStage):void {
			var answer:Answer;
			var posx:Number = quizStage.question.x + quizStage.question.height + 30; 
			var area:Number = (quizStage.stage.stageHeight-30) - posx;
			for (var i:int = 0; i < vo.answerArray.length; i++) {
				answer = new Answer(vo.answerArray[i]);
				quizStage.addChild(answer.visual);
				answer.visual.x = 100;
				answer.visual.y = posx + (area/vo.answerArray.length)*i;
				answer.addEventListener(CheckGroupEvent.SELECTED, onSelected);
				this.answers.push(answer);
				answer = null;
			}
		}
		
		protected function onSelected(pEvent:CheckGroupEvent):void {
			//MonsterDebugger.trace(this, pEvent);
			for (var i:int = 0; i < this.answers.length; i++) {
				if (pEvent.answer != this.answers[i]) {
					this.answers[i].selected = false;
				}
				
			}
		}
		
		public function unselectAll():void {
			for (var i:int = 0; i < this.answers.length; i++) {
				this.answers[i].selected = false;
			}
		}
		
		/**
		 * Função que remove a questão.
		 * @return void
		 * @author caiocesarnevesdeoliveira
		 **/

		public function removeQuestion():void {
			var stageQuiz:QuizStage = answers[0].visual.parent as QuizStage;
			stageQuiz.question.htmlText = "";
			for each (var vo:Answer in answers) {
				
				stageQuiz.removeChild(vo.visual);
				
			}
		}
		
		
	}
}